package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.defence.RtCode;
import com.migo.entity.TActivationEntity;
import com.migo.oss.OSSFactory;
import com.migo.service.SysOssService;
import com.migo.service.TActivationService;
import com.migo.utils.DateUtil;
import com.migo.utils.DecCodeUtil;
import com.migo.utils.IPUtils;
import com.migo.utils.R;
import com.migo.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Wes on 2018/12/25.
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
public class ApiActivationController {


    @Autowired
    OSSFactory ossFactory;

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private TActivationService tActivationService;




    /**
     * 激活
     */
    @IgnoreAuth
    @RequestMapping(value = "proActivation", produces = "application/json;charset=utf-8;")
    public R proActivation(@RequestParam Map<String, String> params, HttpServletRequest request) {
       // String encrypt_str="";
        String address ="";
       /* if(params.containsKey("encryptStr")){
             encrypt_str = params.get("encryptStr");
        }*/
        if(params.containsKey("address")){
            address = params.get("address");
        }
        String userid = params.get("userId");
        String psn = params.get("psn");
        String activation_time = params.get("activationTime");
        R result =  R.ok();

        try {
           // Assert.isBlank(encrypt_str, "encryptStr is null");
            Assert.isBlank(userid, "userId is null");
            Assert.isBlank(psn, "psn is null");
            Assert.isBlank(activation_time, "activationTime is null");

            /* 激活码 pvmg10不使用了，所以注释
             DecCodeUtil dcu = new DecCodeUtil("pv!#!$16");
             String decrypt = dcu.decrypt(encrypt_str);
             TActivationEntity tActivationEntity = tActivationService.queryObjectForEncrypt(decrypt);
             */

            List<TActivationEntity> activationEntityList = tActivationService.queryObjectForPsn(psn);


            if(activationEntityList.size()>0){
                TActivationEntity  tActivationEntity=activationEntityList.get(0);
                int status = tActivationEntity.getStatus();

                tActivationEntity.setStatus(1);
                tActivationEntity.setUserId(userid);
                tActivationEntity.setActivationTime(DateUtil.getNow());
                if(params.containsKey("lonlat")){
                    tActivationEntity.setLonlat(params.get("lonlat"));
                }
                if(params.containsKey("device")){
                    tActivationEntity.setDevicetype(params.get("device"));
                }
                if(params.containsKey("address")){
                tActivationEntity.setActivationAddress(params.get("address"));
                }
    //                    tActivationEntity.setDevicecode(psn);  //更新PSN码
                tActivationEntity.setUserReqIp(IPUtils.getIpAddr(request));

                tActivationService.update(tActivationEntity);
                result.put("msg","设备："+psn+"，激活成功！");
                return result;

            }else {
                TActivationEntity  tActivationEntity= new TActivationEntity();
                tActivationEntity.setStatus(1);
                tActivationEntity.setUserId(userid);
                tActivationEntity.setDevicecode(psn);
                tActivationEntity.setActivationTime(DateUtil.getNow());
                if(params.containsKey("lonlat")){
                    tActivationEntity.setLonlat(params.get("lonlat"));
                }
                if(params.containsKey("device")){
                    tActivationEntity.setDevicetype(params.get("device"));
                }
                if(params.containsKey("address")){
                    tActivationEntity.setActivationAddress(params.get("address"));
                }
                tActivationEntity.setUserReqIp(IPUtils.getIpAddr(request));

                tActivationService.save(tActivationEntity);
                result.put("msg","设备："+psn+"，激活成功！");
                return result;
//                  return R.error(RtCode.ActivationCodeError.getCode(), RtCode.ActivationCodeError.getMsg());
            }
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }
}
